package Core 
{
	import com.greensock.TweenLite;	
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import Fonts.CalibriEmbed;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class Tooltip extends Sprite 
	{
		private var FText:TextField;
		private var FMinY:Number;
		
		public function Tooltip() 
		{
			visible = false;
			alpha = 0;
			FMinY = new Number(-1);
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);						
			FText = new TextField();
			FText.textColor = 0x000000;
			FText.autoSize = TextFieldAutoSize.LEFT;			
			var vFormat:TextFormat = new TextFormat();
			vFormat.font = new CalibriEmbed().fontName;				
			FText.defaultTextFormat = vFormat;
			FText.selectable = false;
			addChild(FText);
			mouseEnabled = false;		
		}
		
		private function OnEnterFrameBordered(e:Event):void 
		{
			if (visible && parent)
			{
				var vScreenOffset:Number = ((stage.stageWidth - 5) - (stage.mouseX + width));				
				x = parent.mouseX + vScreenOffset * int(vScreenOffset < 0);				
				if (FMinY > -1) 				
				{
					if (parent.mouseY > FMinY)
						y = parent.mouseY + 25;
					else
						y = FMinY + 25;
				}
				else
					y = parent.mouseY + 25;
			}
		}
		
		private function OnEnterFrame(e:Event):void 
		{
			if (visible && parent)
			{
				var vScreenOffset:Number = ((stage.stageWidth - 25) - (stage.mouseX + width));				
				x = parent.mouseX + vScreenOffset * int(vScreenOffset < 0);
				y = parent.mouseY + 25;
			}
		}
		
		override public function set visible(value:Boolean):void
		{			
			if (value)
			{
				super.visible = true;
				TweenLite.to(this, 0.2, { alpha:0.85 } );
			}
			else
			{
				TweenLite.to(this, 0.2, { alpha:0, onComplete:OnHide } );
			}
		}
		
		public function OnHide():void
		{
			super.visible = false;
		}
		
		public function set Text(AText:String):void
		{
			FText.htmlText = AText;
			graphics.clear();
			graphics.beginFill(0xFFFFFF, 1);
			graphics.drawRect(0, 0, FText.width, FText.height);
			graphics.endFill();
		}		
				
		public function set MinY(value:Number):void 
		{
			FMinY = value;
			if (FMinY > -1)
			{
				removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
				addEventListener(Event.ENTER_FRAME, OnEnterFrameBordered);
			}
		}
	}
}